<!-- 消息输入框 -->
<template>
  <div id="uesrtext">
    <textarea
      placeholder="按 Ctrl + Enter 发送"
      v-model="content"
      v-on:keyup="addMessage"
    ></textarea>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "uesrtext",
  data() {
    return {
      content: "",
    };
  },
  computed: mapState(["sessions", "currentSessionId"]),
  methods: {
    addMessage(e) {
      if (e.ctrlKey &&e.keyCode === 13 && this.content.length) {
       
        // this.$store.commit("addMessage", this.content);
        // this.content = "";

        if (this.currentSessionId === "-1") {
          this.$message({
            showClose: true,
            message: "请选择用户",
            type: "error",
          });
          return;
        }

        let socketMsg = { msg: this.content, toUser: this.currentSessionId };
        socketMsg.type = 1;
        //console.log("向服务器发送信息:" + JSON.stringify(socketMsg));
        this.$store.dispatch("WEBSOCKET_SEND", JSON.stringify(socketMsg));        
        this.content = "";
      }
    },
  },
};
</script>

<style lang="scss" scoped>
#uesrtext {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 30%;
  border-top: solid 1px #ddd;
  > textarea {
    padding: 10px;
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
  }
}
</style>